package com.zheng.business.web.service;

import com.zheng.business.common.constant.BusinessConstant;
import com.zheng.business.common.constant.BusinessResult;
import com.zheng.business.common.constant.BusinessResultConstant;
import com.zheng.business.dao.model.*;
import com.zheng.business.rpc.api.*;
import com.zheng.business.web.util.StatisticsUtil;
import com.zheng.common.constant.Constant;
import com.zheng.common.util.DateUtil;
import com.zheng.common.util.DateUtils;
import com.zheng.common.util.StringUtil;
import com.zheng.upms.dao.model.UpmsAgent;
import com.zheng.upms.dao.model.UpmsAgentExample;
import com.zheng.upms.rpc.api.UpmsApiService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.MapUtils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author lvg EMIAL：<a href="mailto:weiguo.liang@gh-ca.com">weiguo.liang@gh-ca.com</a> <br>
 * @version 1.0 <br>
 * @Company: GH-CA<br>
 * @ClassName: BusinessAgentHttpTaskService <br>
 * @Description: TODO <br>
 * @date 2019/8/8 17:29<br>
 **/
@Service
public class BusinessMachineHttpTaskService {

    @Autowired
    private BusinessApiService businessApiService;

    @Autowired
    BusinessMachineAmountStatisticsService businessMachineAmountStatisticsService;

    public BusinessResult statisticsMachineAmountByDateTypeAndDate(String dateStr, BusinessConstant.StatisticsDateType dateType) {

        Map<String,Object> params = StatisticsUtil.getStatisticsParams(dateStr,dateType);
        Date currentDate = new Date();

        //雨伞订单时间段统计结果
        List<Map<String,Object>> orderResultList = businessApiService.getMachineOrderAmountByTimeSlot((Date)params.get("beginTime"),(Date)params.get("endTime"));
        for(Map<String,Object> map : orderResultList){
            String machineId = StringUtil.getString(map.get("machine_id"));
            String amount = StringUtil.getString(map.get("sumAmount"));
            if(StringUtils.isNotBlank(machineId) && StringUtils.isNotBlank(amount)){
                BusinessMachineAmountStatistics statistics = new BusinessMachineAmountStatistics();
                statistics.setAmount(new BigDecimal(amount));
                statistics.setCreateTime(currentDate);
                statistics.setMachineCode(machineId);
                statistics.setType(dateType.getValue());
                statistics.setUnit(params.get("unitDateStr").toString());
                businessMachineAmountStatisticsService.insertSelective(statistics);
            }
        }

        return new BusinessResult(BusinessResultConstant.SUCCESS,null);
    }

    public BusinessResult statisticsMachineOrderCountByDateTypeAndDate(String dateStr, BusinessConstant.StatisticsDateType dateType) {

        Map<String,Object> params = StatisticsUtil.getStatisticsParams(dateStr,dateType);
        Date currentDate = new Date();

        //雨伞订单时间段统计结果
        List<Map<String,Object>> orderResultList = businessApiService.getMachineOrderSuccessTotalByTimeSlot((Date)params.get("beginTime"),(Date)params.get("endTime"));
        for(Map<String,Object> map : orderResultList){
            String machineId = StringUtil.getString(map.get("machine_id"));
            String amount = StringUtil.getString(map.get("sumAmount"));
            if(StringUtils.isNotBlank(machineId) && StringUtils.isNotBlank(amount)){
                BusinessMachineAmountStatistics statistics = new BusinessMachineAmountStatistics();
                statistics.setAmount(new BigDecimal(amount));
                statistics.setCreateTime(currentDate);
                statistics.setMachineCode(machineId);
                statistics.setType(dateType.getValue());
                statistics.setUnit(params.get("unitDateStr").toString());
                businessMachineAmountStatisticsService.insertSelective(statistics);
            }
        }

        return new BusinessResult(BusinessResultConstant.SUCCESS,null);
    }
}
